package controller

import (
	"github.com/gin-gonic/gin"
)

type ParamController struct{}

type Search struct {
	Name string `json:"name"`
	Cid  string `json:"cid"`
}

// GetParam Get请求参数
func (p ParamController) GetParam(c *gin.Context) {
	id := c.Param("id")
	ReturnSuccess(c, 0, "success", gin.H{"id": id}, 1)
}

// GetParams Get请求参数（多值）
func (p ParamController) GetParams(c *gin.Context) {
	cid := c.Param("cid")
	name := c.Param("name")
	ReturnSuccess(c, 0, "success", gin.H{"cid": cid, "name": name}, 1)
}

// PostParam Post请求参数
func (p ParamController) PostParam(c *gin.Context) {
	uid := c.PostForm("uid")
	name := c.DefaultPostForm("name", "lisi")
	ReturnSuccess(c, 0, "success", gin.H{"uid": uid, "name": name}, 1)
}

// PostJsonParam Post请求参数（json格式）
func (p ParamController) PostJsonParam(c *gin.Context) {
	param := make(map[string]interface{})
	err := c.BindJSON(&param)
	if err != nil {
		ReturnError(c, 4001, gin.H{"err": err})
		return
	}
	ReturnSuccess(c, 0, "success", param, 1)
}

// PostJsonByStructParam Post请求参数（绑定请求参数结构体获取）（json格式）
func (p ParamController) PostJsonByStructParam(c *gin.Context) {
	search := &Search{}
	err := c.BindJSON(&search)
	if err != nil {
		ReturnError(c, 4001, gin.H{"err": err})
		return
	}
	ReturnSuccess(c, 0, "success", search, 1)
}
